home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / text / hyper / hsc_source.lha / hsc / source / hsclib / input.c < prev    next >
C/C++ Source or Header  |  1996-11-17  |  5KB  |  235 lines

  1. /*
  2.  * hsclib/input.c
  3.  *
  4.  * basic functions for parsing input
  5.  *
  6.  * Copyright (C) 1995,96  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated: 17-Nov-1996
  23.  * created: 29-Jul-1995
  24.  */
  25.  
  26. #include <ctype.h>
  27.  
  28. #include "hsclib/inc_base.h"
  29.  
  30. /*
  31.  *---------------------------
  32.  * overloaded methods for
  33.  * INFILE class
  34.  *---------------------------
  35.  */
  36.  
  37. /*
  38.  * hsc_whtspc
  39.  *
  40.  * decides if an char is a white-space
  41.  *
  42.  * params: ch...char to test
  43.  * result: TRUE, if ch is a 'normal' ch
  44.  *
  45.  * NOTE: this function is used as is_ws-methode
  46.  *       for the INFILE class
  47.  */
  48. BOOL hsc_whtspc(int ch)
  49. {
  50.     if ((ch == ' ')
  51.         || (ch == '\n')
  52. #if (!defined MSDOS || defined HSC_INTO)
  53.         || (ch == '\r')
  54. #endif
  55.         || (ch == '\t')
  56.         )
  57.     {
  58.         return TRUE;
  59.     }
  60.     else
  61.         return FALSE;
  62. }
  63.  
  64. /*
  65.  * hsc_normch
  66.  *
  67.  * decides if an char is an 'normal' char
  68.  *
  69.  * params: ch...char to test
  70.  * result: TRUE, if ch is a 'normal' ch
  71.  *
  72.  * NOTE: this function is used as is_nc-methode
  73.  *       for the INFILE class
  74.  */
  75. BOOL hsc_normch(int ch)
  76. {
  77.     if (((ch >= '0') && (ch <= '9'))
  78.         || ((ch >= 'a') && (ch <= 'z'))
  79.         || ((ch >= 'A') && (ch <= 'Z'))
  80.         || (ch == '_') || (ch == '-') || (ch == '.')
  81.         )
  82.     {
  83.         return TRUE;
  84.     }
  85.     else
  86.         return FALSE;
  87. }
  88.  
  89. /*
  90.  * hsc_normch_tagid
  91.  *
  92.  * decides if an char is an 'normal' char for tagnames
  93.  *
  94.  * params: ch...char to test
  95.  * result: TRUE, if ch is a 'normal' ch
  96.  *
  97.  * NOTE: this function is used as is_nc-methode
  98.  *       for the infile class
  99.  */
  100. BOOL hsc_normch_tagid(int ch)
  101. {
  102.     BOOL found = hsc_normch(ch);
  103.  
  104.     if (!found)
  105.         if (strchr(HSC_TAGID, ch))
  106.             found = TRUE;
  107.  
  108.     return (found);
  109. }
  110.  
  111. /*
  112.  *-------------------------------------
  113.  * read a tag/attribute name from input file
  114.  *-------------------------------------
  115.  */
  116.  
  117. /*
  118.  * infget_tagid
  119.  *
  120.  * read next word from input, but with a
  121.  * different is_nc-methode that also handles
  122.  * the id for hsc-tags (usually "$")
  123.  */
  124. STRPTR infget_tagid(HSCPRC * hp)
  125. {
  126.     INFILE *inpf = hp->inpf;
  127.     STRPTR tagid = NULL;
  128.  
  129.     BOOL(*old_is_nc) (int ch);
  130.  
  131.     old_is_nc = inpf->is_nc;    /* remember old is_nc-methode */
  132.     inpf->is_nc = hsc_normch_tagid;
  133.     tagid = infgetw(inpf);      /* read tagid */
  134.     if (!tagid)
  135.         hsc_msg_eof(hp, "reading tag name");
  136.     inpf->is_nc = old_is_nc;    /* remember old is_nc-methode */
  137.  
  138.     return (tagid);
  139. }
  140.  
  141. /*
  142.  * infget_attrid
  143.  *
  144.  * read next word from input, but with a
  145.  * different is_nc-methode that also handles
  146.  * the id for hsc-attribs (usually "$")
  147.  */
  148. STRPTR infget_attrid(HSCPRC * hp)
  149. {
  150.     INFILE *inpf = hp->inpf;
  151.     STRPTR attrid = NULL;
  152.  
  153.     BOOL(*old_is_nc) (int ch);
  154.  
  155.     old_is_nc = inpf->is_nc;    /* remember old is_nc-methode */
  156.     inpf->is_nc = hsc_normch_tagid;
  157.     attrid = infgetw(inpf);     /* read attrid */
  158.     if (!attrid)
  159.         hsc_msg_eof(hp, "reading attribute name");
  160.     inpf->is_nc = old_is_nc;    /* remember old is_nc-methode */
  161.  
  162.     return (attrid);
  163. }
  164.  
  165. /*
  166.  *-------------------------------------
  167.  * parse simple chars/words
  168.  *-------------------------------------
  169.  */
  170.  
  171. /*
  172.  * parse_wd
  173.  *
  174.  * check if a expected word really occured and
  175.  * display error message if neccessary
  176.  *
  177.  * params: inpf.....input file to read char from
  178.  *         expstr...expected word
  179.  * result: TRUE if successful, FALSE if wrong char found
  180.  */
  181. BOOL parse_wd(HSCPRC * hp, STRPTR expstr)
  182. {
  183.     INFILE *inpf = hp->inpf;
  184.     BOOL value = TRUE;
  185.  
  186.     if (expstr)
  187.     {
  188.         STRPTR nw = infgetw(inpf);
  189.  
  190.         /* check for expeted word */
  191.         if (!nw || upstrcmp(nw, expstr))
  192.         {
  193.             if (!nw)
  194.                 nw = "<EOF>";
  195.  
  196.             hsc_message(hp, MSG_UNEXPT_CH,
  197.                         "expected %q, found %q", expstr, nw);
  198.             value = FALSE;
  199.         }
  200.     }
  201.     else
  202.     {
  203.         panic("no data to expect");
  204.     }
  205.  
  206.     return (value);
  207. }
  208.  
  209. /*
  210.  * parse_eq
  211.  *
  212.  * check for '='
  213.  *
  214.  * params: inpf...input file to read char from
  215.  * result: -1 if successful, 0 if wrong char found
  216.  */
  217. BOOL parse_eq(HSCPRC * hp)
  218. {
  219.     return (parse_wd(hp, "="));
  220. }
  221.  
  222. /*
  223.  * parse_gt
  224.  *
  225.  * check for '>'
  226.  *
  227.  * params: inpf...input file to read char from
  228.  * result: -1 if successful, 0 if wrong char found
  229.  */
  230. BOOL parse_gt(HSCPRC * hp)
  231. {
  232.     return (parse_wd(hp, ">"));
  233. }
  234.  
  235.